from turbogears import controllers, expose, flash, error_handler
from turbogears import validators, validate, identity, redirect

from cherrypy import request, response

from eventscafe import json
from eventscafe.widgets import forms


from model import User, Event, Venue, EventTag


class Root(controllers.RootController):

    @expose(template=".templates.admin")
    def index(self):
        u = User.select()
        return dict(users=u)

    @expose(template=".templates.login")
    def login(self, forward_url=None, previous_url=None, *args, **kw):

        if not identity.current.anonymous \
            and identity.was_login_attempted() \
            and not identity.get_identity_errors():
            raise redirect(forward_url)

        forward_url=None
        previous_url= request.path

        if identity.was_login_attempted():
            msg=_("The credentials you supplied were not correct or "
                   "did not grant access to this resource.")
        elif identity.get_identity_errors():
            msg=_("You must provide your credentials before accessing "
                   "this resource.")
        else:
            msg=_("Please log in.")
            forward_url= request.headers.get("Referer", "/")
            
        response.status=403
        return dict(message=msg, previous_url=previous_url, logging_in=True,
                    original_parameters=request.params,
                    forward_url=forward_url)

    @expose()
    def logout(self):
        identity.current.logout()
        raise redirect("/")
        
    @expose(template='.templates.admin')
    def del_user(self, id):
        u = User(user_id=id)
        